package org.wmine.algo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.ProgressMonitor;
import javax.swing.Timer;

public class ProgressBar extends JFrame //implements ActionListener
{
   
            private Timer activityMonitor;
			private ProgressMonitor progressDialog;
			private SimulatedActivity activity;	
			public ProgressBar()
			{       
					activityMonitor=new Timer(500,new ActionListener(){public void actionPerformed(ActionEvent ae){
					int current=activity.getCurrent();
					progressDialog.setProgress(current);
					if(current>=activity.getTarget()||progressDialog.isCanceled())
					{
						activityMonitor.stop();
						progressDialog.close();
						activity.interrupt();
						JOptionPane.showMessageDialog(null,"Result File Generated");
					}
				}});    
}
void getDialog()
{
            activity=new SimulatedActivity(1000);
	        activity.start();
	        progressDialog=new ProgressMonitor(this,"Processing.....Please Wait",null,0,activity.getTarget());
            activityMonitor.start();
	
}
public static void main(String [] a)
{
		ProgressBar pb=new ProgressBar();
		pb.getDialog();
	}

}

class SimulatedActivity extends Thread
   {
   	public SimulatedActivity(int t)
   	{
   		current=0;
   		target=t;
   		}
   	public int getTarget()
   	{
   		return target;
   		}
   		public int getCurrent()
   		{
   			return current;
   			}
public void run()
{
	while(current<target && !interrupted())
	{
		try{
			
			sleep(50);
			}
			catch(InterruptedException e)
			{
				return;
				}
				current+=30;
		
		}
	}   			
	private int current,target;
	
	
   	}